/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.services.ejb3.facade;

import java.util.ArrayList;

import javax.ejb.Stateless;

import org.apache.log4j.Logger;

import com.phonebooksharing.dto.GenericTransferDTO;
import com.phonebooksharing.dto.ProfileItem;
import com.phonebooksharing.dto.ReturnItem;
import com.phonebooksharing.dto.friend.GroupItem;
import com.phonebooksharing.dto.gallery.ImageGalleryItem;
import com.phonebooksharing.dto.gallery.VideoGalleryItem;
import com.phonebooksharing.dto.gallery.WishlistItem;
import com.phonebooksharing.services.ejb3.utils.IConstants;

/**
 * Stateless EJB Bean with contains the facade logic which performs 
 * the desired lookups and business logic required by its method.
 * 
 * <p> Should always return an object that will be serialized by its 
 * caller and sent back to Android.
 * 
 * @author <a href='mailto:luizrafael@gmail.com'>Rafael Fernandes</a>
 *
 */
@Stateless
public class ProfileFacadeBean extends AbstractFacadeBean implements ProfileFacade {
    private static Logger log = Logger.getLogger(ProfileFacadeBean.class);
    
    public ReturnItem validate(ProfileItem pi) {
        ReturnItem ri;;
        try {
            log.info("Validating profile, username [" + pi.username + "]...");
            ri = new ReturnItem(true);
            ri.setAuthItem("HUIhoPOKQW!@1154vEd");
            log.debug("Profile auth is [" + ri.getAuthItem() + "]");
            
            return ri;
        } catch (Exception e) {
            log.error("Error validating profile...", e);
            ri = new ReturnItem(false);
        }
        
        return ri;
    }

    @Override
    public ReturnItem addAsyncProcessor(GenericTransferDTO gt, int type) {
        log.info("Adding to async processor...");
        return new ReturnItem(true);
    }

    @Override
    public void scheduleAccountActivation(GenericTransferDTO gt) {
        //void - fire and forget
        log.info("Scheduling account activation...");
    }

    @Override
    public ArrayList<ImageGalleryItem> getSharingImages(String authItem) {
        log.info("Retrieving/returning sharing images, authItem [" + authItem + "]...");
        ArrayList<ImageGalleryItem> list = new ArrayList<ImageGalleryItem>();
        
        fillWithImages(list);
        
        return list;
    }

    @Override
    public ArrayList<ImageGalleryItem> getNonSharingImages(String authItem) {
        log.info("Retrieving/returning NON sharing images, authItem [" + authItem + "]...");
        ArrayList<ImageGalleryItem> list = new ArrayList<ImageGalleryItem>();
        
        fillWithImages(list);
        
        return list;
    }

    @Override
    public ArrayList<VideoGalleryItem> getSharingVideos(String authItem) {
        log.info("Retrieving/returning sharing videos, authItem [" + authItem + "]...");
        ArrayList<VideoGalleryItem> list = new ArrayList<VideoGalleryItem>();
        
        fillWithVideos(list);
        
        return list;
    }

    @Override
    public ArrayList<VideoGalleryItem> getNonSharingVideos(String authItem) {
        log.info("Retrieving/returning NON sharing videos, authItem [" + authItem + "]...");
        ArrayList<VideoGalleryItem> list = new ArrayList<VideoGalleryItem>();
        
        fillWithVideos(list);
        
        return list;
    }

    @Override
    public String getImagePath(Long imageID) {
        log.info("Retrieving/returning image path, imageID [" + imageID + "]...");
        final String anID = "3089";
        return String.format(IConstants.ROOT_FOR_IMAGES + "%s.jpg", anID);//the path should be valid in the server with an image on.
    }

    @Override
    public ReturnItem startSharingImage(ArrayList<ImageGalleryItem> list) {
        log.info("Start sharing a collection of images and returning OK...");
        return new ReturnItem(true);
    }

    @Override
    public ReturnItem stopSharingImage(ArrayList<ImageGalleryItem> list) {
        log.info("Stop sharing a collection of images and returning OK...");
        return new ReturnItem(true);
    }

    @Override
    public ArrayList<ImageGalleryItem> addImagesToGallery(String authItem, ArrayList<ImageGalleryItem> list) {
        log.info("Adding images to the gallery, authItem [" + authItem + "]...");
        
        int i = 0;
        for(ImageGalleryItem ig : list) {
            ig.photoID = ++i;
        }
        
        return list;
    }

    @Override
    public void setImagePathToImageGallery(Long imageID, String imagePath) {
        //void - fire and forget
        log.info("Set image path to images on gallery, imageID [" + imageID + "], imagePath [" + imagePath + "]...");
    }

    @Override
    public ArrayList<Integer> getImageGalleryDeviceIdList(String authItem) {
        log.info("Retrieving/returning images gallery ids from device, authItem [" + authItem + "]...");
        
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            list.add(i);
        }
        
        return list;
    }
    
    @Override
    public ArrayList<Integer> getVideoGalleryDeviceIdList(String authItem) {
        log.info("Retrieving/returning video gallery ids from device, authItem [" + authItem + "]...");
        
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0; i < NUMBER_OF_ITEMS_IN_COLLECTION; i++) {
            list.add(i);
        }
        
        return list;
    }

    @Override
    public String getImagePathAndIncreaseViewCounter(Long imageID) {
        log.info("Retrieving/returning image path and increasing the view counter, imageID [" + imageID + "]...");

        final String anID = "3089";
        return String.format(IConstants.ROOT_FOR_IMAGES + "%s.jpg", anID);//the path should be valid in the server with an image on.
    }

    @Override
    public ArrayList<VideoGalleryItem> addVideosToGallery(String authItem, ArrayList<VideoGalleryItem> list) {
        log.info("Adding videos to the gallery, authItem [" + authItem + "]...");
        
        int i = 0;
        for(VideoGalleryItem ig : list) {
            ig.videoID = ++i;
        }
        
        return list;
    }

    @Override
    public void setVideoPathToVideoGallery(Long videoID, String videoPath) {
        //void - fire and forget
        log.info("Set video path to video on gallery, videoID [" + videoID + "], videoPath [" + videoPath + "]...");
    }

    @Override
    public void setVideoThumbPathToVideoGallery(Long videoID, String videoThumbPath) {
        //void - fire and forget
        log.info("Set video path to video on gallery, videoID [" + videoID + "], videoThumbPath [" + videoThumbPath + "]...");
    }

    @Override
    public ReturnItem startSharingVideo(ArrayList<VideoGalleryItem> list) {
        log.info("Start sharing a collection of videos and returning OK...");
        
        return new ReturnItem(true);
    }

    @Override
    public ReturnItem stopSharingVideo(ArrayList<VideoGalleryItem> list) {
        log.info("Stop sharing a collection of videos and returning OK...");

        return new ReturnItem(true);
    }

    @Override
    public ArrayList<ImageGalleryItem> browseImageGallery(String authItem, int contactID, int page, int pageSize) {
        log.info("Browsing image gallery, authItem [" + authItem + "], contactID [" + contactID + "], page [" + page + "], pageSize [" + pageSize + "]...");
        
        ArrayList<ImageGalleryItem> list = new ArrayList<ImageGalleryItem>();
        
        fillWithImages(list);
        
        return list;
    }

    @Override
    public ArrayList<VideoGalleryItem> browseVideoGallery(String authItem, int contactID, int page, int pageSize) {
        log.info("Browsing video gallery, authItem [" + authItem + "], contactID [" + contactID + "], page [" + page + "], pageSize [" + pageSize + "]...");
        
        ArrayList<VideoGalleryItem> list = new ArrayList<VideoGalleryItem>();
        
        fillWithVideos(list);
        
        return list;
    }
    
    @Override
    public ArrayList<WishlistItem> browseWishlistGallery(String authItem, int contactID, int page, int pageSize) {
        log.info("Browsing wishlist gallery, authItem [" + authItem + "], contactID [" + contactID + "], page [" + page + "], pageSize [" + pageSize + "]...");
        
        ArrayList<WishlistItem> list = new ArrayList<WishlistItem>();
        
        fillWithWishlist(list);
        
        return list;
    }

    @Override
    public String getVideoThumbPath(Long id) {
        log.info("Retrieving/returning video thumb path, id [" + id + "]...");

        final String anID = "3089";
        return String.format(IConstants.ROOT_FOR_VIDEOS + "%s.jpg", anID);//the path should be valid in the server with an image on.
    }

    @Override
    public String getVideoPathAndIncreaseViewCounter(Long id) {
        log.info("Retrieving/returning video path and increasing the view counter, id [" + id + "]...");
        
        final String anID = "3089";
        return String.format(IConstants.ROOT_FOR_VIDEOS + "%s.jpg", anID);//the path should be valid in the server with an image on.
    }

    @Override
    public ReturnItem addProductToWishlist(String authItem, ArrayList<WishlistItem> list) {
        log.info("Adding wishlist items to the gallery, authItem [" + authItem + "]...");
        
        return new ReturnItem(true);
    }

    @Override
    public ArrayList<WishlistItem> getNonSharingWishlist(String authItem) {
        log.info("Retrieving/returning sharing wishlist, authItem [" + authItem + "]...");
        
        ArrayList<WishlistItem> list = new ArrayList<WishlistItem>();
        
        fillWithWishlist(list);
        
        return list;
    }

    @Override
    public ArrayList<WishlistItem> getSharingWishlist(String authItem) {
        log.info("Retrieving/returning NON sharing wishlist, authItem [" + authItem + "]...");
        
        ArrayList<WishlistItem> list = new ArrayList<WishlistItem>();
        
        fillWithWishlist(list);
        
        return list;
    }

    @Override
    public ReturnItem startSharingWishlist(ArrayList<WishlistItem> list) {
        log.info("Start sharing a collection of wishlist items and returning OK...");
        
        return new ReturnItem(true);
    }

    @Override
    public ReturnItem stopSharingWishlist(ArrayList<WishlistItem> list) {
        log.info("Stop sharing a collection of wishlist items and returning OK...");
        
        return new ReturnItem(true);
    }

    @Override
    public ReturnItem updatePrivacy(String authItem, ArrayList<GroupItem> list) {
        log.info("Updating privacy, authItem [" + authItem + "]...");
        
        return new ReturnItem(true);
    }
}
